#include "createdevicemanipulator.h"
#include "qsignalslotcenter.h"
#include <osg\Camera>
#include "scene.h"
//#include "terrainmanager.h"

namespace ControlCenter
{
	CreateDeviceManipulator::CreateDeviceManipulator(osg::Camera* maincamera)
	{
		_mainCamera = maincamera;
		_usePickManager = false;
	}

	CreateDeviceManipulator::~CreateDeviceManipulator()
	{}

	bool CreateDeviceManipulator::testCreateObject()
	{
		if(_operatorObjectVector.size() != 1)
		{
			return false;
		}
		_createActor = dynamic_cast<VrDataCore::DrawableActor*>(_operatorObjectVector.at(0));
		return true;
	}

	void CreateDeviceManipulator::mouseMove(const osgGA::GUIEventAdapter* ea0,const osgGA::GUIEventAdapter* ea1)
	{
		if(!testCreateObject())
			return;
		float f1 = ea0->getX();
		float f2 = ea0->getY();
		osg::Matrixd viewmat = getInverseMatrix();
		osg::Matrixd projmat = _mainCamera->getProjectionMatrix();
		osg::Matrixd vpmat = _mainCamera->getViewport()->computeWindowMatrix();
		osg::Matrixd mat = viewmat*projmat*vpmat;
		osg::Vec3 worldpos0 = osg::Vec3(f1,f2,0)*mat.inverse(mat);
		osg::Vec3 worldpos1 = osg::Vec3(f1,f2,1)*mat.inverse(mat);
		_mouseCurrentpos = osg::Vec3((0-worldpos1.z())*(worldpos1.x()-worldpos0.x())/(worldpos1.z()-worldpos0.z())+worldpos1.x(),
			(0-worldpos1.z())*(worldpos1.y()-worldpos0.y())/(worldpos1.z()-worldpos0.z())+worldpos1.y(),0);
		//float heightz = HTSDTerrainManager::GetInstance().getTerrainHeight(_mouseCurrentpos.x(),_mouseCurrentpos.y());
		_mouseCurrentpos.z() = 0;//heightz;
		_createActor->setTranslation(_mouseCurrentpos);
	}

	void CreateDeviceManipulator::mouseClick(const osgGA::GUIEventAdapter* ea0,const osgGA::GUIEventAdapter* ea1,
			const osgGA::GUIEventAdapter* key0,const osgGA::GUIEventAdapter* key1)
	{}

	void CreateDeviceManipulator::mouseRelease(const osgGA::GUIEventAdapter* ea0,const osgGA::GUIEventAdapter* ea1,
			const osgGA::GUIEventAdapter* key0,const osgGA::GUIEventAdapter* key1)
	{
		if(!testCreateObject())
			return;
		QVector<CoreBase::ActorBase*> createobjects;
		switch(ea0->getButton())
		{
		case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
			createobjects.push_back(_createActor);
			QSignalSlotCenter::QSignalSlotCenter::GetInstance().emitActionBeforeCreateDevices(createobjects);
			break;
		default:
			break;
		}
	}

	void CreateDeviceManipulator::enableManipulator()
	{}

	void CreateDeviceManipulator::disenableManipulator()
	{
		if(_createActor!=NULL)
		{
			_createActor->getDrawable()->getSceneParent()->removeDrawable(_createActor->getDrawable());
			_createActor = NULL;
		}
		clearOperatorObjectVector();
	}

	void CreateDeviceManipulator::keyDown(const osgGA::GUIEventAdapter* ea0,osgGA::GUIActionAdapter& us)
	{
		int v = ea0->getKey() + 0xff00;
		switch(v)
		{
		case osgGA::GUIEventAdapter::KEY_Escape:
			QSignalSlotCenter::QSignalSlotCenter::GetInstance().emitManipulatorModeChange(CLASS_MANIPULATOR_MAIN);
			break;
		default:
			break;
		}
	}

	void CreateDeviceManipulator::computePositionByScreenPoint(osg::Vec3& eye,osg::Vec3& center,osg::Vec3& up)
	{
		float dis;
		getLookAt(eye,center,up,dis);
		osg::Vec3 interpolation = _mouseCurrentpos - eye;
		float al = interpolation.x()/sqrt(interpolation.x()*interpolation.x()+interpolation.y()*interpolation.y());
		std::vector<osg::Vec3> newdata;
		eye = _mouseCurrentpos;
		center = _mouseCurrentpos + osg::Vec3(2000.0f*al,2000.0f*sqrt(1-al*al),0.0f);
		up = osg::Vec3(0.0f,0.0f,1.0f);
	}
}